package Service.Teacher.Imp;

import Dao.Admin.GradingManagementDao;
import Dao.DaoFactory;
import Dao.Grade.GroupGradeDao;
import Dao.Role.ReplyGroupDao;
import Dao.TitleAndPaper.PaperDao;
import Entity.Grade.GroupGrade;
import Entity.MyTableData;
import Entity.ReplyGroup;
import Entity.Role.Teacher;
import Entity.TitleAndPaper.Paper;
import Service.Teacher.ReplyGroupService;
import Util.DBUtil;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

public class ReplyGroupServiceImp implements ReplyGroupService {
    private final GradingManagementDao gradingManagementDao;
    private final Connection conn;
    private final ReplyGroupDao replyGroupDao;
    private final PaperDao paperDao;
    private final GroupGradeDao groupGradeDao;

    public ReplyGroupServiceImp() {
        conn = DBUtil.getConnByDruid();
        gradingManagementDao = DaoFactory.getInstance("gradingManagementDao");
        replyGroupDao = DaoFactory.getInstance("replyGroupDao");
        paperDao = DaoFactory.getInstance("paperDao");
        groupGradeDao = DaoFactory.getInstance("groupGradeDao");

    }

    /**
     * @return 1001 成功  1002 失败 1003 答辩教师人数不足4人 无法分配
     */
    @Override
    public Integer allotGroup(String type) {
        int code = 1002;
        //查询所有的答辩教师
        Random random = new Random();
        List<Teacher> teachers = gradingManagementDao.getAllByRole("答辩组教师", conn);
        if (teachers.size() >= 4) {
            //4个人一组  求到的需要的分组组数
            int groupSum = (int) Math.floor((double) teachers.size() / 4);
            HashMap<Integer, ArrayList<Teacher>> groups = new HashMap<>();  // 存放所有的答辩小组的集合
            for (int i = 1; i <= groupSum; i++) {  //控制答辩组数
                ArrayList<Teacher> group = new ArrayList<>(); //一个答辩小组
                for (int j = 0; j < 4; j++) { //控制每个小组的人数 为 4个
                    int randSum = random.nextInt(teachers.size());
                    group.add(teachers.get(randSum));
                    teachers.remove(randSum);
                }
                groups.put(i, group);
            }
            //设置答辩组信息
            List<ReplyGroup> replyGroups = new ArrayList<>();
            //得到分组后面 确定答辩组长 判断是否查询分组
            if ("2".equals(type)) { //如果是重新分组则需要删除全部分组信息
                replyGroupDao.delGroup(conn);
            }

            for (Integer key : groups.keySet()) {
                ReplyGroup replyGroup = new ReplyGroup();
                replyGroup.setGroupNO(String.valueOf(key)); //答辩小组编号
                ArrayList<Teacher> teachers1 = groups.get(key); // 成员
                int groupLeader = random.nextInt(4);  //随机生成一个答辩组长
                replyGroup.setGroupLeader(teachers1.get(groupLeader).getTNO());
                StringBuilder groupMember = new StringBuilder();
                for (Teacher teacher : teachers1) {
                    groupMember.append(teacher.getName());
                    groupMember.append("   ");
                }
                replyGroup.setGroupMember(groupMember.toString());
                replyGroups.add(replyGroup); // 往答辩组列表添加信息
            }
            for (ReplyGroup replyGroup : replyGroups) {
                replyGroupDao.insertGroup(replyGroup, conn);
            }
            code = 1001;
        } else {
            code = 1003;
        }
        DBUtil.closeConn(conn);
        return code;
    }

    /**
     * 学生分配答辩小组
     *
     * @return 1001 成功  1002 还没有答辩教师组 1003 失败
     */
    @Override
    public Integer allotStudent() {
        int code = 1003;
        //查询已有答辩小组
        Random random = new Random();
        List<ReplyGroup> replyGroups = replyGroupDao.getAllGroup(conn);
        if (replyGroups.size() == 0) {
            code = 1002;
        } else {
            //存在答辩小组  查询所有论文处于在定稿的学生
            List<Paper> papers = paperDao.getAllPaperGroup(conn);
            //删除答辩小组成绩表的记录
            groupGradeDao.delGrading(conn);
            //答辩组的人数设置为0
            replyGroupDao.setNOStu(conn);
            for (Paper paper : papers) {
                int randSum = random.nextInt(replyGroups.size());
                paper.setReplyGroup(String.valueOf(replyGroups.get(randSum).getGroupNO()));
                //设置答辩小组编号
                paperDao.updateGroup(paper, conn);
                //更新答辩小组的人数
                replyGroupDao.updateStuSumById(replyGroups.get(randSum).getId(), conn);
                //往答辩小组成绩表插入数据
                String SNO = paper.getSno();
                String TNO = replyGroups.get(randSum).getGroupLeader();
                GroupGrade grade = new GroupGrade();
                grade.setSno(SNO);
                grade.setTno(TNO);
                groupGradeDao.insertSNOAndTNO(SNO, TNO, conn);
            }
            code = 1001;
        }
        DBUtil.closeConn(conn);
        return code;
    }


    @Override
    public MyTableData getAllGroup() {
        List<ReplyGroup> replyGroups = replyGroupDao.getAllGroup(conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", replyGroups.size(), replyGroups);
    }

    @Override
    public Boolean addTimeAndAddress(Integer id, String replyTime, String replyAddress) {
        Boolean flag = replyGroupDao.addTimeAndAddress(id, replyTime, replyAddress, conn) == 1;
        DBUtil.closeConn(conn);
        return flag;
    }
}
